package com.songcha.library_common.ui.view.compose

import androidx.annotation.ColorRes
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun InputField(
    modifier: Modifier = Modifier,
    value: String,
    onValueChange: (String) -> Unit,
    placeholder: @Composable (() -> Unit)? = null,
    drawableStart: @Composable (() -> Unit)? = null,
    drawableStartPadding: Dp = 8.dp,
    drawableEnd: @Composable (() -> Unit)? = null,
    drawableEndPadding: Dp = 8.dp,
    readOnly: Boolean = false,
    maxLines: Int = 1,
    textStyle: TextStyle = TextStyle.Default,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    cursorBrush: Color = colorResource(id = com.songcha.library_common.R.color.primary),
    alignment: Alignment = Alignment.CenterStart,
) {

    BasicTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier,
        textStyle = textStyle,
        keyboardOptions = keyboardOptions,
        keyboardActions = keyboardActions,
        singleLine = maxLines == 1,
        maxLines = maxLines,
        cursorBrush = SolidColor(cursorBrush),
        visualTransformation = visualTransformation,
        decorationBox = @Composable { coreTextField ->
            Row(verticalAlignment = Alignment.CenterVertically) {
                if (drawableStart != null) {
                    drawableStart()
                    Spacer(modifier = Modifier.width(drawableStartPadding))
                }
                Box(modifier = Modifier
                    .then(modifier)
                    .weight(1f), contentAlignment = alignment) {
                    coreTextField()
                    if (placeholder != null && value.isEmpty()) {
                        placeholder()
                    }
                }
                if (drawableEnd != null) {
                    Spacer(modifier = Modifier.width(drawableEndPadding))
                    drawableEnd()
                }
            }
        },
        readOnly = readOnly
    )

}